package com.module.assignment;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import org.phprpc.util.PHPSerializer;
import org.xsocket.connection.INonBlockingConnection;

import com.command.Group;
import com.connect.agreement.Packet;
import com.game.base.SimulationObject;
import com.game.base.SobFactory;
import com.game.manager.CommManager;
import com.game.manager.SobManager;
import com.game.nonperfomers.Item;
import com.game.nonperfomers.item.Assignment;
import com.game.nonperfomers.item.AssignmentGoal;
import com.game.nonperfomers.item.AssignmentReward;
import com.game.performers.Actor;
import com.module.itemeditor.CfgItem;
import com.module.itemeditor.StringProperty;

/**
 * 委派管理器（做任务啦）
 * @author chzcb
 * @email chzcb2008@gmail.com
 * @date 2011-9-8
 *
 */
public class AssignmentManager {
	public static final byte GetMyAssignments = 1;
	public static final byte UpdateAssignment = 2;
	public static final byte AcceptAssignment = 3;
	
	
	/**
	 * 所有的委派库
	 */
	protected static final Hashtable<Integer, Assignment> allbaseassignments = new Hashtable<Integer, Assignment>();
	
	/**
	 * 所有的委派目标库
	 */
	protected static final Hashtable<Integer, AssignmentGoal> allgoals = new Hashtable<Integer, AssignmentGoal>();
	
	protected static final Hashtable<Integer, AssignmentReward> allrewards = new Hashtable<Integer, AssignmentReward>();
	
	protected static final Hashtable<Integer, AssignmentSender> allSenders = new Hashtable<Integer, AssignmentSender>();
	
	/**
	 * 启动委派管理器
	 */
	public static void setup(){
		for(CfgItem item : CfgItem.getAllCfgItem()){
			if(item.getProperty("type").equals("任务目标")){
				addGoal(item);
			} else if(item.getProperty("type").equals("任务奖励")){
				addReward(item);
			} else if(item.getProperty("type").equals("任务")){
				addAssignment(item);
			}
		}
	}
	
	/**
	 * 缓存所有的委派目标对象
	 */
	protected static void addGoal(CfgItem item){
		AssignmentGoal goal = new AssignmentGoal();
		goal.cfg_id = item.id;
		goal.initInstance();
		allgoals.put(goal.cfg_id, goal);
	}
	
	/**
	 * 缓存所有的委派奖励
	 */
	protected static void addReward(CfgItem item){
		AssignmentReward reward = new AssignmentReward();
		reward.cfg_id = item.id;
		reward.initInstance();
		allrewards.put(reward.cfg_id, reward);
	}
	
	/**
	 * 缓存所有的委派
	 */
	protected static void addAssignment(CfgItem item){
		Assignment assignment = new Assignment();
		assignment.cfg_id = item.id;
		assignment.initInstance();
		allbaseassignments.put(assignment.cfg_id, assignment);
	}
	
	/**
	 * 接受委派
	 * @param rid
	 * @param cfg_id
	 * @return
	 */
	public static Assignment acceptAssign(int rid,int cfg_id){
		Assignment assignment = allbaseassignments.get(cfg_id);
		assignment = (Assignment)SobManager.get().cloneSob(assignment, rid);
		//获得任务物品
		assignment.mtime = new Date().getTime();
		assignment.accept = 1002;
		assignment.initInstance();
		assignment.setDirty(true);
		assignment.notifyObservers();
		return assignment;
	}
	
	/**
	 * 取得当前角色接受的所有委派
	 * @param rid
	 * @return
	 */
	public static List<SimulationObject> getAssignments(int rid){
		String[] filters = new String[1];
		filters[0] = Assignment.class.getName();
		return SobManager.get().getChilds(rid, filters);
	}
	/**
	 * 当前角色是否拥有委派发送器
	 * @param actor
	 * @return
	 */
	public static boolean hasAsgmSenderByActor(Actor actor){
		String[] filters = new String[1];
		filters[0] = AssignmentSender.class.getName();
		List<SimulationObject> ret = SobManager.get().getChilds(actor.id, filters);
		if(ret.size()>0){
			return true;
		}
		else
			return false;
	}
	/**
	 * 当前角色是否拥有委派接收器
	 * @param actor
	 * @return
	 */
	public static boolean hasAsgmReceiverByActor(Actor actor){
		String[] filters = new String[1];
		filters[0] = AssignmentReceiver.class.getName();
		List<SimulationObject> ret = SobManager.get().getChilds(actor.id, filters);
		if(ret.size()>0){
			return true;
		}
		else
			return false;
	}
	
	public static void test(){
	}
	
	public static void update(Packet packet, INonBlockingConnection nbc){
		switch(packet.getCmd()){
		case GetMyAssignments:
			
			break;
		case UpdateAssignment:
			
			break;
		case AcceptAssignment:
			
			break;
		}
	}
}
